package com.csdj.user.service;

import com.csdj.beans.entity.User;
import com.csdj.beans.entity.UserExample;
import com.csdj.beans.interfaces.user.IUserService;
import com.csdj.common.constant.CommonConstant;
import com.csdj.common.constant.RedisConstant;
import com.csdj.common.utils.RandomUtil;
import com.csdj.middleware.RedisService;
import com.csdj.user.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
@CacheConfig(cacheNames = "user")
public class UserService implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisService redisService;



    @Override
    public User login(String username,String password){
        final UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<User> list = userMapper.selectByExample(userExample);
        if(list != null && list.size() == 1){
            String token = RandomUtil.genToken();
            User user = list.get(0);
            //保存token 到redis
            redisService.setString(RedisConstant.TOKEN_PREFIX+user.getId(),token);
            //缓存用户信息
            redisService.setObject(RedisConstant.CACHE_USER+user.getId(),user);
            //删除密码
            user.setPassword("");
            user.setToken(token);
            return user;
        }
        return null;
    }


    @Override
    public List<User> getList(Byte userType, Byte status, String role, Integer page, Integer rows) {
        UserExample userExample = new UserExample();
        userExample.setLimit(rows);
        userExample.setOffset((page-1)*rows);
        final UserExample.Criteria criteria = userExample.createCriteria();
        if(userType != null){
            criteria.andUserTypeEqualTo(userType);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        if(role != null){
            criteria.andRoleEqualTo(role);
        }
        return userMapper.selectByExample(userExample);
    }

    @Override
    public long getListTotal(Byte userType, Byte status, String role) {
        UserExample userExample = new UserExample();
        final UserExample.Criteria criteria = userExample.createCriteria();
        if(userType != null){
            criteria.andUserTypeEqualTo(userType);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        if(role != null){
            criteria.andRoleEqualTo(role);
        }
        return userMapper.countByExample(userExample);
    }

    @Override
    @Cacheable(key = "#root.methodName+#userId",condition = "#result != null ")
    public User getById(int userId){
        return userMapper.selectByPrimaryKey(userId);
    }
    @Cacheable(value= "user",key="#root.methodName+#name")
    public List<User> getByName(String name){
        System.out.println("从数据库查询");
        final UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameLike("%"+name+"%");
        return userMapper.selectByExample(userExample);
    }
    public List<User> getByType(Byte start,Byte end){
        final UserExample userExample = new UserExample();
        userExample.createCriteria().andUserTypeBetween(start,end);
        return userMapper.selectByExample(userExample);
    }

    /**
     * 当前用户的权限
     * @return
     */
    @Override
    public String getCurUserRole() {

        return null;
    }

    @Override
    public String getCurUserRole(int uid) {
        final User user = getById(uid);
        if(user != null){
            return user.getRole();
        }
        return null;
    }


    /**
     * 生成二维码， 两分钟后失效
     * @return
     */
    @Override
    public String getQrLoginCode(String sessionId) {
        String code = RandomUtil.genToken();
        redisService.setString(sessionId,code,120);
        return code;
    }


    @Override
    public User checkQrCode(int uid,String sessionId,String code) {
        final String qrcode = redisService.getString(sessionId);
        if(StringUtils.hasText(qrcode) && qrcode.equals(code)){
            String token = redisService.getString(RedisConstant.TOKEN_PREFIX+uid);
            final User user = new User();
            user.setToken(token);
            user.setId(uid);
            return user;
        }
        return null;
    }
}
